var ports = require("../../utils/ports.js")
var app = getApp()
var flag = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberOrderDetail: null,
    hidden: false,
    goodsQTY: 0,
    returnDescription: '',
    shopDetail: null,
    sendType: null
  },
  rating: function () {
    wx.navigateTo({
      url: '../rating/rating',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    const memberOrderID = options.id;
    if (!memberOrderID) {
      wx.showToast({
        title: '缺少参数',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      return;
    }
    that.setData({
      memberOrderID:memberOrderID
    })    
    wx.showNavigationBarLoading();    
  },
  getMemberOrderDetail(memberOrderID) {
    const that = this;
    ports.ModuleMemberOrder.getMemberOrderDetail({ "memberOrderID": memberOrderID }, that, (res) => {
      wx.hideNavigationBarLoading();
      if (res.data.header.code == 0) {
        const memberOrderDetail = res.data.body.data;
        memberOrderDetail.payTotal = memberOrderDetail.payTotal.toFixed(2);
        that.setData({
          memberOrderDetail
        })
      }
    })
  },
  toenrolldd: function (e) {
    var id = e.currentTarget.dataset.text
    wx.navigateTo({
      url: '../confirm_order/confirm_order?memberOrderID=' + id,
    })
  },
  /**
   * 评价订单
   */
  toEvaluated(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?id=' + id,
    })
  },

  call(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset['phone']
    })
  },

  onShow: function () {
    flag = false;
    this.getMemberOrderDetail(this.data.memberOrderID);
  },
  confirmMemberOrder(e) {
    wx.showLoading();
    const that = this;
    const memberOrderID = e.currentTarget.dataset.text;
    ports.ModuleMemberOrder.confirmMemberOrder({ "memberOrderID": memberOrderID }, that, (res) => {
      wx.hideLoading();
      if (res.data.header.code === 0) {
        wx.showToast({
          title: '收货成功',
          icon: 'success'
        })
        const currentPags = getCurrentPages();
        const prev = currentPags[currentPags.length - 2];
        const myOrderList = prev.data.myOrderList;
        const index = myOrderList.findIndex(c => c.memberOrderID === memberOrderID);
        const {
          navData,
          currentSendTypeIndex,
          currentStatus
        } = prev.data;
        const {
          status
        } = navData[currentSendTypeIndex].child[currentStatus];
        if (status === '') { // tab等于全部时
          const key = `myOrderList[${index}].status`;
          prev.setData({
            [key]: 8
          })
        } else {
          myOrderList.splice(index, 1);
          prev.setData({
            myOrderList
          })
        }
        setTimeout(() => {
          wx.navigateBack();
        }, 2000);
      } else {
        wx.showToast({
          title: res.data.header.msg,
          icon: 'none'
        })
      }
    })
  },

  removeOrder() {
    const that = this;
    var memberOrderDetail = that.data.memberOrderDetail;
    wx.showModal({
      title: '提示',
      content: '您真的要取消订单吗？',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍后...',
            mask: true
          })
          ports.ModuleMemberOrder.cancelMemberOrder({ "memberOrderID": memberOrderDetail.id }, that, (res) => {
            wx.hideNavigationBarLoading();
            if (res.data.header.code === 0) {
              memberOrderDetail.status = 2;
              that.setData({
                memberOrderDetail: memberOrderDetail
              })
              wx.showToast({
                title: '订单已取消',
                icon: 'success',
                mask: true,
                duration: 2000
              })
              setTimeout(() => {
                wx.navigateBack();
              }, 2000)
            } else {
              wx.hideLoading();
              wx.showToast({
                title: '取消失败',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  }
})